package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.mapper.CheckItemMapper;
import com.health.pojo.CheckItem;
import com.health.request.CheckItemDto;
import com.health.response.PageResult;
import com.health.response.Result;
import com.health.service.CheckItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author : Areli
 * @date : 2020/4/12 20:47
 */
/**
 * 业务层要做的事
 * 1.参数校验
 * 2.业务代码流程
 * 3.封装执行结果
 */
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 查询所有
     * @return 检查项列表
     */
    @Override
    public Result<List<CheckItem>> findAll() {
        //2.业务代码流程
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        if (checkItemList == null || checkItemList.size() == 0){
            return new Result<>("查无结果");
        }
        //3.封装执行结果
        Result<List<CheckItem>> result = new Result<>(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        return result;
    }

    /**
     * 分页条件查询
     * @param checkItemDto 条件对象
     * @param page         当前页码
     * @param pageSize     每页显示条数
     * @return
     */
    @Override
    public Result search(CheckItemDto checkItemDto, Integer page, Integer pageSize) {
        //1.参数校验
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        CheckItem checkItem = new CheckItem();
        BeanUtils.copyProperties(checkItemDto,checkItem);
        //2.执行业务逻辑
        PageHelper.startPage(page,pageSize);
        Page<CheckItem> pageList = (Page<CheckItem>) checkItemMapper.search(checkItem);
        if (pageList == null || pageList.getTotal() == 0){
            return new Result<>("查询失败");
        }
        //3.封装返回结果
        PageResult<CheckItem> pageResult = new PageResult<>();
        pageResult.setTotal(pageList.getTotal());
        pageResult.setRows(pageList.getResult());
        return new Result(pageResult);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Result<CheckItem> findOne(Integer id) {
        CheckItem item = checkItemMapper.findOne(id);
        if (item == null){
            return new Result<>("查无信息");
        }
        return new Result<>(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,item);
    }

    /**
     * 根据ID修改
     * @param id
     * @param checkItem
     * @return
     */
    @Override
    public Result<CheckItem> edit(Integer id, CheckItem checkItem) {
        //查询id是否存在
        CheckItem one = checkItemMapper.findOne(id);
        if (one == null){
            return new Result<>("无当前检查项可以修改");
        }
        //执行修改
        checkItem.setId(id);
        checkItemMapper.edit(checkItem);
        return new Result<>(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }

    /**
     * 新增
     * @param checkItem
     * @return
     */
    @Override
    public Result<CheckItem> add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
        return new Result<>(checkItem);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        checkItemMapper.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
    }
}
